package org.easy.wechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.easy.wechat.dto.WechatSession;
import org.easy.wechat.dto.WxaCodeUnLimitReq;
import org.apache.commons.lang3.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Li chaojie
 * @Date: 2022/11/15
 * @Description:
 */

public class ThirdWechatServiceImpl implements ThirdWechatService {

    private final static String API_JSCODE2SESSION = "https://api.weixin.qq.com/sns/jscode2session";

    private final static String ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";

    private final static String GET_WXACODE_UNLIMIT = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";

    private final static String GET_WXACODE = "https://api.weixin.qq.com/wxa/getwxacode?access_token=";

    private final static String GET_USERPHONENUMBER = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    private final DynamicWechatProperties dynamicWechatProperties;

    public ThirdWechatServiceImpl(DynamicWechatProperties dynamicWechatProperties) {
        this.dynamicWechatProperties = dynamicWechatProperties;
    }


    /**
     * 工具js code 获取用户的uuid openId
     * js code2session
     *
     * @param appId
     * @param jsCode js代码
     * @return {@link WechatSession}
     */
    @Override
    public WechatSession jsCode2session(String appId, String jsCode) {
        Map<String, String> param = getAppIdParamMap(appId);
        param.put("js_code", jsCode);
        param.put("grant_type", "authorization_code");
        String data = HttpUtil.sendGet(API_JSCODE2SESSION, null, param);
        return JSON.parseObject(data, WechatSession.class);
    }


    @Override
    public String getWxaCodeUnLimit(WxaCodeUnLimitReq wxaCodeUnLimitReq) {
        String token = getAccessToken(wxaCodeUnLimitReq.getAppId());
        String host = GET_WXACODE_UNLIMIT + token;
        Map<String, Object> param = new HashMap<>();
        param.put("scene", wxaCodeUnLimitReq.getScene());
        if (!ObjectUtils.isEmpty(wxaCodeUnLimitReq.getPage())) {
            param.put("page", wxaCodeUnLimitReq.getPage());
        }
        if (!ObjectUtils.isEmpty(wxaCodeUnLimitReq.getCheckPath())) {
            param.put("check_path", wxaCodeUnLimitReq.getCheckPath());
        }
        if (!ObjectUtils.isEmpty(wxaCodeUnLimitReq.getEnvVersion())) {
            param.put("env_version", wxaCodeUnLimitReq.getEnvVersion());
        }
        if (!ObjectUtils.isEmpty(wxaCodeUnLimitReq.getWidth())) {
            param.put("width", wxaCodeUnLimitReq.getWidth());
        }
        String result = HttpUtil.postJsonBase64MiniFile(host, null, param);
        return result;
    }

    @Override
    public String getWxacode(String appId, String path) {
        String token = getAccessToken(appId);
        String host = GET_WXACODE + token;
        Map<String, Object> param = new HashMap<>();
        param.put("path", path);
        String result = HttpUtil.postJsonBase64MiniFile(host, null, param);
        return result;
    }

    @Override
    public String getUserPhoneNumber(String appId, String code) {
        String token = getAccessToken(appId);
        String host = GET_USERPHONENUMBER + token;
        Map<String, String> param = new HashMap<>();
        param.put("code", code);
        String message = HttpUtil.postJson(host, null, param);
        JSONObject jsonObject = JSONObject.parseObject(message);
        return jsonObject.getJSONObject("phone_info").getString("phoneNumber");
    }

    /**
     * 获取token
     *
     * @param appId
     * @return
     */
    private String getAccessToken(String appId) {
        Map<String, String> param = getAppIdParamMap(appId);
        param.put("grant_type", "client_credential");
        String message = HttpUtil.sendGet(ACCESS_TOKEN, null, param);
        JSONObject jsonObject = JSONObject.parseObject(message);
        String accessToken = jsonObject.getString("access_token");
        return accessToken;
    }

    /**
     * 包装请求参数
     *
     * @param appId
     * @return
     */
    private Map<String, String> getAppIdParamMap(String appId) {
        WechatProperties wechatProperties = dynamicWechatProperties.getProperties().get(appId);
        if (wechatProperties == null) {
            throw new RuntimeException("WeChat appId 对应的配置为空" + appId);
        }
        Map<String, String> param = new HashMap<>();
        param.put("appid", appId);
        param.put("secret", wechatProperties.getSecret());
        return param;
    }


}
